/* Constructor */
BelMenuItem = {}

/* Initialization */
BelMenuItem.Initialize				= function( self, parentMenu, target, ... )

	/* Base stuff */
	BelCheckButton.Initialize( self, ... )
	
	/* Default values */
	self.AllowUncheck = false
	self.LinkedMenu = nil
	self.ParentMenu = parentMenu
	
	/* Target is a function to be called on click */
	if IsFunction( target ) then
		DEBUG:Msg( "Target is a function", "BelMenuItem.Initialize")
		self.OnSelected = target
		self.OnSelectedTargetParams = ...
		
	/* Target is a menu to open on click */
	elseif IsValidInstance( target, BelMenu ) then
		DEBUG:Msg( "Target is a menu", "BelMenuItem.Initialize" )
		self.LinkedMenu = target
		
	/* Target is a menu contents */
	elseif IsValidInstance( target, BelMenuContents ) then
		Msg( "Target is a BelMenuContents", "BelMenuItem.Initialize" )
		self.Value = target
	end
	
	/* Events */
	BelControls.ClearHooks( "OnPointed", self )
	self.OnPointed = function( self ) return BelControls.Call( self, "OnPointed", self ) end
	
	/* Remove default mouse hooks */
	BelControls.ClearHooks( "OnMousePressed", self )
	
	/* Add new mouse hooks */
	BelControls.AddHook( "OnMousePressed", self, self.MousePressed )
	BelControls.AddHook( "OnCursorEntered", self, self.Pointed )
end

/* Mouse pressed */
BelMenuItem.MousePressed			= function( self )
	
	/* Do nothing if already checked & uncheck is not allowed */
	if self.Checked and not self.AllowUncheck then return end

	/* Do the base stuff */
	BelCheckButton.MousePressed( self )
	
	/* If there is no target function, just skip */
	if not self.OnSelected then
		return
	end
	
	/* Ensure target function */
	if not IsFunction( self.OnSelected ) then
		DEBUG:ErrMsg( "The OnSelected hook must be a valid function", "BelMenuItem.MousePressed" )
		return
	end
	
	/* Call the function if checked */
	if self.Checked then
		DEBUG:Msg( "Raising selected event", "BelMenuItem.MousePressed" )
		self.OnSelected( self.OnSelectedTargetParams )
	end
end

/* When mouse enters, trigger 'OnPointed' */
BelMenuItem.Pointed = function( self )

	/* Debug output */
	DEBUG:Msg( "Item pointed", "BelMenuItem.Pointed" )

	/* Notify the menu that this item got pointed */
	self.OnPointed( self )
end

/* Register the component */
vgui.Register( "BelMenuItem", BelMenuItem, "BelCheckButton" )